package com.zhyonk.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhyonk.config.propreties.BaiduProperties;
import com.zhyonk.config.propreties.TianxinProperties;
import com.zhyonk.domain.StarUser;
import com.zhyonk.mapper.StarUserMapper;
import com.zhyonk.service.AsyncService;
import com.zhyonk.service.FaceService;
import com.zhyonk.service.TianxinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

@Service("tianxinService")
public class TianxinServiceImpl implements TianxinService {

    @Override
    public String articleGen(String someThings) {
        String httpUrl = "http://api.tianapi.com/txapi/cangtoushi/index?key=APIKEY&word=天行数据";

        HashMap<String, Object> map = new HashMap<>();
        map.put("key",TianxinProperties.getUserKey());
        if (someThings.length() > 8){
            someThings = someThings.substring(8);
        }
        String[] strArr1 = new String[]{"帅","美","好","靓","爱"};
        String[] strArr2 = new String[]{"帅气","好运","幸福","美好","美丽"};
        int randomInt = RandomUtil.randomInt(1, 5);
        if (someThings.length() == 2){
            //最后补两位，随机
            someThings = someThings + strArr2[randomInt];
        }

        if (someThings.length() == 3){
            //最后补一位，随机
            someThings = someThings + strArr1[randomInt];
        }


        map.put("word",someThings);
        map.put("len",0);
        HttpResponse execute = HttpRequest.post(httpUrl)
                .form(map)
                .execute();
        String body = execute.body();
        System.out.println(body);
        JSONObject object = JSON.parseObject(body);
        try{
            JSONArray newlist = object.getJSONArray("newslist");
            JSONObject jsonObject = newlist.getJSONObject(0);
            return jsonObject.getString("list");
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
    }

}
